//
//  VCOfLoveListen.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/27.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfLoveListen.h"
#import "XYMacro.h"

@interface VCOfLoveListen () <TopPageBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) DiscoverTopPageBar *topBar;
@property (nonatomic, strong) UICollectionView *collectionViewOfContent;
@property (nonatomic, strong) NSUserDefaults *user;
@property (nonatomic, strong) NSArray *picNameArr;
@property (nonatomic, strong) NSString *target_id;

@property (nonatomic, strong) LoveListenModelFirst *firstModel;
@property (nonatomic, strong) LoveListenModelSecond *secondModel;

@end

@implementation VCOfLoveListen

- (void)viewDidLoad {
    [super viewDidLoad];
    _user = [NSUserDefaults standardUserDefaults];
        _picNameArr = @[@"logout_bg_0", @"logout_bg_1"]
    ;
    
    [self configNavigationController];
    [self configViewController];
    [self createKVO];
}


- (void)handleData {
    
    //订阅
    NSString *str1 = @"json=%7B%22cat_id%22%3A%220%22%2C%22orderby%22%3A2%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A1%2C%22session%22%3A%7B%22uid%22%3A%22";
    NSString *str2 = @"%22%2C%22sid%22%3A%22c4aca220e2b147afb71fcf2980ea755d%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%22";
    NSString *str3 = @"%22%7D";
    NSString *body = [[[[str1 stringByAppendingString:_target_id] stringByAppendingString:str2] stringByAppendingString:_target_id] stringByAppendingString:str3];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _firstModel  = [LoveListenModelFirst modelWithDictionary:result];
        [_collectionViewOfContent reloadData];
        
    } andFailureBlock:^(NSError *error) {}];
    
    
    //收藏
    NSString *str4 = @"json=%7B%22pagination%22%3A%7B%22count%22%3A50%2C%22page%22%3A1%7D%2C%22session%22%3A%7B%22uid%22%3A%22";
    NSString *str5 = @"%22%2C%22sid%22%3A%22c4aca220e2b147afb71fcf2980ea755d%22%7D%7D";
    NSString *body1 = [[str4 stringByAppendingString:_target_id] stringByAppendingString:str5];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/user/collects.jhtml" andBody:body1 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _secondModel = [LoveListenModelSecond modelWithDictionary:result];
        
    } andFailureBlock:^(NSError *error) {}];


}


- (void)configNavigationController {
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    titleLabel.text = @"爱听";
    titleLabel.textColor = [UIColor whiteColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)configViewController {
    [self createTopBar];
    [self createContentView];
    
}

- (void)createTopBar {
    _topBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) withTitleArr:@[@"订阅", @"收藏"]];// @"下载", @"历史"]];
    _topBar.delegate = self;
    [self.view addSubview:_topBar];
    
}

- (void)createContentView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewOfContent = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49) collectionViewLayout:layout];
    _collectionViewOfContent.dataSource = self;
    _collectionViewOfContent.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
    _collectionViewOfContent.delegate = self;
    _collectionViewOfContent.pagingEnabled = YES;
    _collectionViewOfContent.scrollEnabled = NO;
    [self.view addSubview:_collectionViewOfContent];
        [_collectionViewOfContent registerClass:[LoveListenCollectionViewCellOfContentViewFirst class] forCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewFirst"];
    [_collectionViewOfContent registerClass:[LoveListenCollectionViewCellOfContentViewSecond class] forCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewSecond"];
    [_collectionViewOfContent registerClass:[LoveListenCollectionViewCellOfContentViewThird class] forCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewThird"];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (![_user objectForKey:@"password"]) {
            LoveListenCollectionViewCellOfContentViewSecond *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewSecond" forIndexPath:indexPath];
        cell.imageView.image = [UIImage imageNamed:[_picNameArr objectAtIndex:indexPath.item]];
        return cell;
    } else {
        if (indexPath.item == 0) {
            LoveListenCollectionViewCellOfContentViewFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewFirst" forIndexPath:indexPath];
            cell.model = _firstModel;
            return cell;
        } else {
            LoveListenCollectionViewCellOfContentViewThird *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoveListenCollectionViewCellOfContentViewThird" forIndexPath:indexPath];
            cell.model = _secondModel;
            return cell;
            
        }
    }
}

#pragma mark ----------- KVO 键值观察 -----------------
- (void)createKVO {
    //KVO
    [_collectionViewOfContent addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

- (void)dealloc {
    [_collectionViewOfContent removeObserver:self forKeyPath:@"contentOffset"];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 2);
    // 修改Redline位置.
    self.topBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}




- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
    [UIView animateWithDuration:0.3 animations:^{
    _collectionViewOfContent.contentOffset = CGPointMake(indexPath.row * SCREEN_WIDTH, 0);
        }];    
}


- (void)viewWillAppear:(BOOL)animated {
    if ([_user objectForKey:@"password"]) {
        _target_id = [_user objectForKey:@"target_id"];
        [self handleData];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
